package com.yyf.springboot2.controller;

import com.yyf.springboot2.common.ResultBean;
import com.yyf.springboot2.entity.TUser;
import com.yyf.springboot2.service.TUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.awt.SunHints;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/tUserScoreChange")
public class tUserScoreChange extends BaseController{
    @Resource
    private TUserService userService;


    @PostMapping("/ScoreExchange")
    @ResponseBody
    public ResultBean ScoreExchange(@RequestParam(value = "token") String token,
                                    @RequestParam(value = "amount")Integer amount,
                                    @RequestParam(value = "type")Integer type){
        Integer userId = 1;
        //       try {
//         String   uId = AuthToken.decrypt(token,1);
//         userId = Integer.valueOf(uId);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if(userId == null){
//            return  ResultBean.build(104,"token验证失败，请重新获取token",null);
//        }
        if(type == null || amount == null){
            return  ResultBean.build(401,"token验证失败，请重新获取token",null);
        }
        //获取会员账户余额
        TUser userAmount =  userService.getUserAmount(userId);

        if(type > 0 && amount> 0 ){
            if(type ==1){
                if(userAmount.getMoneyMy() < amount){
                    return  ResultBean.build(401,"账户余额不足",null);
                }
                //获取相应的转换比例,
                Double bili = 0.5;
                //获取转换倍数限制
                Double beishu = 100.0;

             if(amount % beishu != 0){
                 return  ResultBean.build(401,"转换金额需要是"+beishu+"的倍数",null);
             }
             if(amount > userAmount.getMoneyMy()){
                 return  ResultBean.build(401,"转换金额大于账户余额",null);
             }
                Map<String,Object> map = new HashMap <>();
                Double    newMoney = userAmount.getMoneyMy() - amount;
                Double newMoneyScore = userAmount.getMoneyScore() + (amount*bili);
                map.put("newMoney",newMoney);
                map.put("userId",userId);
                map.put("newMoneyScore",newMoneyScore);
                //修改账户金额
               Integer result =  userService.moneyScoreChange(map);
               if(result > 0){
                   return  ResultBean.build(200,"转换成功",null);
               }

            }else if(type ==2){
                if(userAmount.getMoneyScore() < amount){
                    return  ResultBean.build(401,"账户积分不足",null);
                }
                //获取相应的转换比例,
                Double bili = 0.5;
                //获取转换倍数限制
                Double beishu = 100.0;

                if(amount % beishu != 0){
                    return  ResultBean.build(401,"转换金额需要是"+beishu+"的倍数",null);
                }
                if(amount > userAmount.getMoneyMy()){
                    return  ResultBean.build(401,"转换金额大于账户余额",null);
                }

                Map<String,Object> map = new HashMap <>();
                Double    newMoney = userAmount.getMoneyMy() + (amount*bili);
                Double newMoneyScore = userAmount.getMoneyScore() - amount;
                map.put("newMoney",newMoney);
                map.put("userId",userId);
                map.put("newMoneyScore",newMoneyScore);
                //修改账户金额
                Integer result =  userService.moneyScoreChange(map);
                if(result > 0){
                    return  ResultBean.build(200,"转换成功",null);
                }

            }else{
                return  ResultBean.build(200,"转换成功",null);
            }

        }else{
            return  ResultBean.build(401,"参数错误,请重新输入",null);
        }

        return  ResultBean.build(501,"未知错误",null);


    }


    @PostMapping("/scoreTransfer")
    @ResponseBody
    public  ResultBean scoreTransfer(@RequestParam(value = "token") String token,
                                     @RequestParam(value = "amount")Integer amount,//转账金额
                                     @RequestParam(value = "type")Integer type, //转账类型
                                     @RequestParam(value = "toUser")String toUser){
        Integer userId = 1;
        //       try {
//         String   uId = AuthToken.decrypt(token,1);
//         userId = Integer.valueOf(uId);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if(userId == null){
//            return  ResultBean.build(104,"token验证失败，请重新获取token",null);
//        }
        if(toUser == null || toUser.equals("") || amount == null){
            return  ResultBean.build(401,"参数错误,请重新填写",null);
        }
        TUser toUserInfo = userService.getUserByUsername(toUser);
        //获取会员账户余额
        TUser userAmount =  userService.getUserAmount(userId);

        //判断是否可以转账，存在团队推荐关系
        if(toUserInfo == null ){
            return  ResultBean.build(401,"参数错误,请重新填写",null);
        }
        if(type > 0 && amount> 0 ){
            if(type ==1){
                if(amount > userAmount.getMoneyMy()){
                    return  ResultBean.build(401,"转账金额大于账户余额",null);
                }
                //手续费比例
                Double shui = 0.05;
                Map<String,Object> map = new HashMap <>();
                map.put("money",amount);
                map.put("userId",userId);
                map.put("shui",shui);
                map.put("toUserId",toUserInfo.getId());
                map.put("type",1);
                //修改账户金额
                Integer result =  userService.moneyScoreTransfel(map);
                if(result > 0){
                    return  ResultBean.build(200,"转账成功",null);
                }

            }else if(type ==2){

                if(amount > userAmount.getMoneyScore()){
                    return  ResultBean.build(401,"转账金额大于账户余额",null);
                }
                //手续费比例
                Double shui = 0.05;
                Map<String,Object> map = new HashMap <>();
                map.put("money",amount);
                map.put("userId",userId);
                map.put("shui",shui);
                map.put("toUserId",toUserInfo.getId());
                map.put("type",2);
                //修改账户金额
                Integer result =  userService.moneyScoreTransfel(map);
                if(result > 0){
                    return  ResultBean.build(200,"转账成功",null);
                }
            }else{
                return  ResultBean.build(200,"转账成功",null);
            }

        }else{
            return  ResultBean.build(401,"参数错误,请重新输入",null);
        }




        return null;
    }



}
